package com.bytz.modules.cms.reseller.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.constant.CommonConstant;
import com.bytz.common.system.query.MPJLambdaWrapperEx;
import com.bytz.common.util.mentity.EntityMergeInfoV2;
import com.bytz.common.util.mentity.util.MergeEntityOptions;
import com.bytz.common.util.mentity.util.MergeEntityUtil;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.reseller.entity.ResellerSales;
import com.bytz.modules.cms.reseller.mapper.ResellerSalesMapper;
import com.bytz.modules.cms.reseller.model.ResellerModel;
import com.bytz.modules.cms.reseller.model.ResellerSalesModel;
import com.bytz.modules.cms.reseller.model.ResellerSalesProductLine;
import com.bytz.modules.cms.reseller.service.IResellerSalesService;
import com.bytz.modules.cms.user.entity.CmsSysUser;
import com.bytz.modules.system.entity.SysUser;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 莱宝CMS—经销商管理—经销商/销售人员关联表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ResellerSalesServiceImpl extends ServiceImpl<ResellerSalesMapper, ResellerSales> implements IResellerSalesService {

    @Override
    public List<ResellerSalesModel> getSalesByResellerId(String resellerId) {
        MPJLambdaWrapperEx<ResellerSales> queryWrapper = new MPJLambdaWrapperEx<>();

        return baseMapper.selectJoinList(ResellerSalesModel.class, queryWrapper
                .selectAll(ResellerSales.class)
                .selectAs(CmsSysUser::getWorkNo, ResellerSalesModel::getSalesExternalId)
                .innerJoin(CmsSysUser.class, on -> on
                        .eq(ResellerSales::getSalesId, CmsSysUser::getId)
                        .eq(ResellerSales::getDelFlag, CommonConstant.DEL_FLAG_0)
                        .eq(CmsSysUser::getDelFlag, CommonConstant.DEL_FLAG_0))
                .eq(ResellerSales::getResellerId, resellerId)
        );
    }

    @Override
    public ResellerSalesModel getSalesByResellerIdAndSaleId(String saleId, String resellerId) {
        MPJLambdaWrapperEx<ResellerSales> queryWrapper = new MPJLambdaWrapperEx<>();
        return baseMapper.selectJoinOne(ResellerSalesModel.class, queryWrapper
                .selectAll(ResellerSales.class)
                .selectAs(CmsSysUser::getWorkNo, ResellerSalesModel::getSalesExternalId)
                .innerJoin(CmsSysUser.class, on -> on
                        .eq(ResellerSales::getSalesId, CmsSysUser::getId)
                        .eq(CmsSysUser::getDelFlag, CommonConstant.DEL_FLAG_0)
                )
                .eq(ResellerSales::getResellerId, resellerId)
                .eq(ResellerSales::getSalesId, saleId)
        );
    }

    @Override
    public List<ResellerSales> getByResellerId(String resellerId) {
        return lambdaQuery()
                .eq(ResellerSales::getResellerId, resellerId)
                .list();
    }

    @Override
    public ResellerSalesProductLine getSalesAndProductLine(String salesId, String resellerId) {
        return baseMapper.selectJoinOne(ResellerSalesProductLine.class,
                new MPJLambdaWrapper<ResellerSales>()
                        .selectAssociation(ProductLine.class, ResellerSalesProductLine::getProductLine)
                        .selectAssociation(CmsSysUser.class, ResellerSalesProductLine::getSeller)
                        .innerJoin(CmsSysUser.class, CmsSysUser::getId, ResellerSales::getSalesId)
                        .leftJoin(ProductLine.class, ProductLine::getDeptId, CmsSysUser::getCurrentDeptId)
                        .eq(ResellerSales::getSalesId, salesId)
                        .eq(ResellerSales::getResellerId, resellerId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSalesByResellerModel(ResellerModel saveRo) {

        Set<ResellerSales> salesSet = saveRo.getLineInfos().stream().flatMap(info -> {
            if (info.getSaleList() == null) {
                return Stream.of();
            }
            return info.getSaleList().stream();
        }).collect(Collectors.toSet());
        salesSet.forEach(sale -> sale.setResellerId(saveRo.getId()));

        EntityMergeInfoV2<ResellerSales> saleMergeInfo = MergeEntityUtil.calculateEntityMergeInfo(
                salesSet,
                getByResellerId(saveRo.getId()),
                MergeEntityOptions.<ResellerSales>builder().keyExtractor(ResellerSales::getSalesId).build()
        );

        MergeEntityUtil.doDbMerge(saleMergeInfo, this);
    }
}